;$(function(){
   
   var opts=this.opts;
   
   var supportFlag = true;
   $(document).on("click","a[rel='support']",function(e){
   	   var url=$(this).attr("data-support-url");           
       var $_self=$(this),$_num=$_self.find("span");
       var $_oldNum = $_num.text();
       
       var isResult=Ace.chkIsLogin(this,e);
       if(isResult==false){
       	  return false;
      }
       
           if($_self.hasClass("a-support-current")){
             // $_num.text("unlinking");
           }else{
             // $_num.text("linking");
           }
       if(supportFlag)
       {
    	   supportFlag = false;
    	   $.getJSON(url,function(data){
    		   if(data.success){
    			   // 设值
    			   $_self.find("span").text(parseInt($_oldNum) + parseInt(data.num));
    			   if($_self.hasClass("a-support-current")){
    				   $_self.removeClass("a-support-current").addClass("a-support");
    				   $_self.attr("data-is-support",false);
    				   $_self.attr("data-support-url",$_self.attr("data-support-url").replace('/-1/','/1/'));
    			   }else{
    				   $_self.removeClass("a-support").addClass("a-support-current");
    				   $_self.attr("data-is-support",true);
    				   $_self.attr("data-support-url",$_self.attr("data-support-url").replace('/1/','/-1/'));
    			   }
    			   supportFlag = true;
    		   }
    	   });
       }
   });
   
   var collectFlag = true;
    $(document).on("click","a[rel='collect']",function(e){
    		 var isResult=Ace.chkIsLogin(this,e);
           if(isResult==false){
           	  return false;
          }
        var url=$(this).attr("data-collect-url");
        var $_self=$(this);
        
        if(collectFlag)
        {
        	collectFlag = false;
        	$.getJSON(url,function(data){
        		if(data.success){
        			// 设值
        			if(data.isCollect){
        				$_self.removeClass("a-collect").addClass("a-collect-current").find("span").text("已收藏");
        				$_self.attr("data-is-collect",true);
        				$_self.attr("data-collect-url",$_self.attr("data-collect-url").replace('/1/','/-1/'));
        			}else{
        				$_self.removeClass("a-collect-current").addClass("a-collect").find("span").text("收藏");
        				$_self.attr("data-is-collect",false);
        				$_self.attr("data-collect-url",$_self.attr("data-collect-url").replace('/-1/','/1/'));
        			}
        			collectFlag = true;
        		}
        	});
        }
    });
    
    $(document).on("click",".J_more_up",function(e){
    	var pageNumber = $("#pageNumber").val();
    	if (parseInt(pageNumber) < 3) 
    	{
    		$(".J_more_up").hide();
    	}
    	$(".J_msg_"+parseInt(pageNumber)).hide();
    	
    	if (pageNumber <= $("#totalPages").val()) 
    	{
    		$(".J_more_down").show();
    	}
    	
    	$("#pageNumber").val(parseInt($("#pageNumber").val()) - 1);
    });
    
    $(document).on("click",".J_more_down",function(e){
    	var articleId =$(this).attr("artId");
    	var $_self = $(this);
    	var pageNumber = parseInt($("#pageNumber").val()) + 1;
    	if ($(".J_msg_"+pageNumber).length>0) 
    	{
    		$(".J_msg_"+pageNumber).show();
    		
    		$("#pageNumber").val(pageNumber);
    		
    		if ($("#totalPages").val() == pageNumber) 
    		{
    			$(".J_more_down").hide();
    		}
    		if (pageNumber > 1) 
    		{
    			$(".J_more_up").show();
    		}
    	}
    	else
    	{
    		appendComments(articleId,pageNumber);
    	}
    	
    });
    
});

function appendComments(articleId,pageNumber){
	$.getJSON("/comment/"+articleId+"/"+pageNumber,function(data){
		$.each(data,function(index,item)
				{
			var createTime = new Date(item.createTime).toLocaleString();
			var tpl =
				'<li class="media J_msg_'+pageNumber+'">' +
				'<a href="/show/user_detail/'+item.user.id+'" class="pull-left img-rounded-small">';
				if (item.user.headImage!=null && item.user.headImage != "") 
				{
					tpl +='    <img width="50" height="50" src="'+item.user.headImage+'" class="media-object img-box-a">';
				}
				else
				{
					tpl +='    <img width="50" height="50" src="http://static.aceona.com/V3/assets/img/head_50.jpg" class="media-object img-box-a">';
				}
				
				tpl +='</a>' +
				'<div class="media-body line_22">' +
				'    <h3><a class="a_red_light" href="/show/user_detail/'+item.user.id+'" target="_blank">'+item.user.nickName+'</a>';
			
			if(item.parentUserName!=null)
			{
				tpl += '&nbsp;&nbsp;回复&nbsp;&nbsp;<a class="a_red_light" href="/show/user_detail/'+item.parentUserId+'" target="_blank">'+item.parentUserName+"</a>";
			}
			
			tpl += '<a href="/dialog/articleComplaintForm/';
			if(item.parentUserName!=null)
				tpl += '3';
			else
				tpl += '2';
			
			tpl += '/'+item.id+'/" id="complaint_'+item.id+'" class="a-xb"  data-chk-login="true" data-target="#modal_complaint_dialog" data-toggle="modal" data-width="365" data-destory-inner="true" ><i class="icon"></i>举报</a>' +
			'    </h3>' +
			'    <p class="f_gray_dark">'+item.content+'</p>' +
			'    <p><span class="m_r_12">'+createTime +'</span>' +
			'    <a href="javascript:void(0);" class="a-feedback" parentId = "'+item.id+'" userId = "'+item.user.id+'" userName="'+item.user.nickName+'"><i class="icon"></i>回复</a></p>' +
			'</div>'+
			'</li>';
			$("#J_commet_list").append(tpl);
			$("#pageNumber").val(pageNumber);
			if ($("#totalPages").val() == pageNumber) 
			{
				$(".J_more_down").hide();
			}
			if (pageNumber > 1) 
			{
				$(".J_more_up").show();
			}
			
			});
			replayComment();
	});
}

var commentSubmitFlag = true;
function detailCommentSubmit(obj,event)
{
	var event=event||windoiw.event;
	var isResult=Ace.chkIsLogin(this,event);
  	if(isResult==false){
  	  return false;
	}
   	
   	var content = $("#articleCommentContent").val().trim();
	var hashId = $("#articleHashId").val();
	if (content.getStringLength() > 400) 
	{
		jAlert("评论内容太长！");
		return false;
	}
	else if(content.getStringLength() < 2 || content == '请输入评论')
	{
		jAlert("请输入评论内容！");
		return false;
	}
   	
   	var userName = $(this).attr("username");
   	
   	if(commentSubmitFlag)
   	{
   		commentSubmitFlag = false;
   		Ace.denyButton(this);
   		$.ajax({
   			url:"/dialog/addcomment?_"+Date.parse(new Date()),
   			type: "POST",
   			data: {
   				articleCommentContent:$("#articleCommentContent").val(),
   				articleHashId:$("#articleHashId").val(),
   				parentCommentHashId:$("#parentCommentHashId").val(),
   				parentUserId:$("#parentUserId").val()
   			},
   			success:function(data){
   				if(data!=null)
   				{
   					$("#commentsDiv").empty().append(data);
   					$("#parentCommentHashId").val("");
   					$("#parentUserId").val("");
   					$("#articleCommentParentUserName").val("");
   					$("#articleCommentContent").attr("placeholder","请输入评论");
   					$("#articleCommentContent").val("");
   					commentSubmitFlag = true;
   					
   					$.ajax({
		       			url:"/dialog/detailcomment/?_"+Date.parse(new Date()),
		       			type: "POST",
		       			data: {
		       				articleId:$("#articleHashId").val()
		       			},
		       			success:function(data){
		       				if(data!=null)
		       				{
		       					$("#artDetailComment").empty().append(data);
		       					replayComment();
		       				}
		       			}
		       		});
   				}
   			}
   		});
   	}
	
	
	
	
	
    //var event=EventUtil.getEvent();
//	var event=event||windoiw.event;
//	var isResult=Ace.chkIsLogin(this,event);
//  	if(isResult==false){
//  	  return false;
//	}
//  	Ace.denyButton(obj);
//	var content = $("#articleCommentContent").val().trim();
//	var hashId = $("#articleHashId").val();
//	if (content.getStringLength() > 400) 
//	{
//		jAlert("评论内容太长！");
//		return false;
//	}
//	else if(content.getStringLength() < 2 || content == '请输入评论')
//	{
//		jAlert("请输入评论内容！");
//		return false;
//	}
//	
//	$("#articleCommentBO").submit();
}

